﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommerceSampleCode.CommerceTransaction;
using CommerceSampleCode.CommerceTestDataProvider;
using Microsoft.Commerce.Proxy.TransactionService.v201001;
using CommerceSampleCode.CommercePaymentInstrument;
using CommercePIProxy = Microsoft.Commerce.Proxy.PaymentInstrumentService;
using CommerceAccountProxy = Microsoft.Commerce.Proxy.AccountService;

namespace WebParts
{
    public partial class Subscription : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String CommercePaymentInstrumentUrl = (String)Application["CommercePaymentInstrumentUrl"];
            String CertLocation = (String)Application["Certlocation"];
            CommercePaymentInstrumentServiceAccessor paymentInstrumentService = new CommercePaymentInstrumentServiceAccessor(CommercePaymentInstrumentUrl, CertLocation);
            paymentInstrumentService.Initialize();

            String acountID = (String)Session["accountID"];
            CommerceAccountProxy.Identity requester = (CommerceAccountProxy.Identity)Session["requester"];
            CommercePIProxy.CallerInfo callerInfo = CommercePaymentInstrumentDataProvider.CreateCallerInfo(requester, acountID);

            CommercePIProxy.PaymentInstrument[] piInfos = paymentInstrumentService.GetPaymentInstruments(callerInfo, null);

            piList.Items.Clear();
            if (piInfos != null)
            {
                foreach (CommercePIProxy.PaymentInstrument pi in piInfos)
                {
                    ListItem item = new ListItem() { Text = pi.PaymentMethodID, Value = pi.PaymentMethodID };
                    piList.Items.Add(item);
                }
            }
        }

        protected void buy_Click(object sender, EventArgs e)
        {
            String CommerceTransactionUrl = (String)Application["CommerceTransactionUrl"];
            String CertLocation = (String)Application["Certlocation"];
            CommerceTransactionServiceAccessor transactionService = new CommerceTransactionServiceAccessor(CommerceTransactionUrl, CertLocation);
            transactionService.Initialize();

            BillingInfo billingInfo = new BillingInfo();
            billingInfo.BillingMode = BillingMode.ImmediateSettle;
            RegisteredPaymentMethod paymentMethod = new RegisteredPaymentMethod();
            paymentMethod.PaymentMethodID = piList.Items[0].Value;
            Response.Write(piList.SelectedIndex);
            billingInfo.PaymentMethod = new PaymentMethod[] { paymentMethod };

            string accountID = (string)Session["accountID"];
            CommerceAccountProxy.Identity requester = (CommerceAccountProxy.Identity)Session["requester"];
            Microsoft.Commerce.Proxy.TransactionService.v201001.CallerInfo callerInfo = CommerceTransactionDataProvider.CreateCallerInfo(requester, accountID);

            Marketplace marketPlace = new Marketplace();
            marketPlace.MarkerplaceGuid = new Guid("e57d1b94-9770-4302-8f75-0e409fbf9aa6");
            marketPlace.MarketplaceName = "test_spsp_css";

            PurchaseContext purchaseContext = new PurchaseContext();
            purchaseContext.ComputeOnly = false;
            purchaseContext.TimeStamp = DateTime.Now;

            PurchaseInfoInput purchaseInfo = new PurchaseInfoInput();
            purchaseInfo.ExternalPurchaseId = "1";
            purchaseInfo.Title = "Purchase Title";

            PurchaseBundleInput purchaseBundleInput = new PurchaseBundleInput();
            purchaseBundleInput.Title = "Bundle Title";
            purchaseBundleInput.Amount = 15.0m;
            purchaseBundleInput.Currency = currency.Text;
            purchaseBundleInput.Description = "Bundle Description";
            purchaseBundleInput.ExternalBundleCatalogId = "1";
            purchaseBundleInput.ExternalBundleId = Guid.NewGuid().ToString();

            PurchaseNewSubscriptionProductItemInput item = new PurchaseNewSubscriptionProductItemInput();
            item.Title = title.Text;
            item.Description = desc.Text;
            item.ExternalProductItemId = "9568F033-0EC5-435F-A159-3ABEBB665620";
            item.ProductItemCatalogId = "9568F033-0EC5-435F-A159-3ABEBB665620";
            item.ProductType = "Online";
            item.RevenueInfo = new RevenueInfo();
            item.RevenueInfo.RevenueAllocationPriceType = RevenueAllocationPriceType.Amount;
            item.RevenueInfo.RevenueAllocationAmount = 15.0m;
            item.RevenueInfo.RevenueSKU = "74U-00003";
            item.RevenueInfo.FinancialReportingAllocationPriceType = FinancialReportingAllocationPriceType.Amount;
            item.SubscriptionFriendlyName = "name";
            item.SupportTitle = "title2";
            item.SupportDescription = "desc2";
            
            ReferralInfo referralInfo = new ReferralInfo();
            referralInfo.CreatedDateTime = DateTime.Now;
            referralInfo.PartnerId = Guid.NewGuid();
            referralInfo.PartnerName = partner.Text;
            item.ReferralInfoSet = new ReferralInfo[] { referralInfo };

            purchaseBundleInput.PurchaseProductItemInputSet = new PurchaseProductItemInput[] { item };

            purchaseInfo.PurchaseBundleInputSet = new PurchaseBundleInput[] { purchaseBundleInput };

            PurchaseOutput purchaseResult = transactionService.Purchase(billingInfo, callerInfo, marketPlace, purchaseContext, purchaseInfo);
            string purchaseID = purchaseResult.PurchaseInfoOutput.PurchaseId;
            Session["purchaseID"] = purchaseID;

            Response.Redirect("ShowPurchases.aspx");

        }
    }
}